<?php
declare (strict_types = 1);

namespace app\course\service\coupon\admin;

use app\common\exception\LogicException;
use app\common\service\BaseService;
use app\course\logic\coupon\admin\CouponUser as CouponUserLogic;
use app\user\logic\user\admin\Account;
use think\facade\Request;

/**
 * 卡券
 */
class CouponUser extends BaseService
{
    public function grant()
    {
        $coupon_id = Request::post('coupon_id/d', 0);
        $phone     = Request::post('phone/s', '');
        if (!$coupon_id || !$phone) {
            throw new LogicException('卡券发放失败');

        }

        $user = app(Account::class)->where('phone', $phone)->find();
        if (!$user) {
            throw new LogicException('所发放用户不存在');
        }

        app(CouponUserLogic::class)->grant($coupon_id, $user);

        return $this->parseToData([], 1, '卡券发放成功');
    }

}
